//
//  DYOrderViewController.m
//  DYHospital
//
//  Created by 胡金东 on 16/3/23.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "DYOrderViewController.h"
#import "DYLeftModel.h"
#import "DYRightCollModel.h"
#import "DYLeftTableCell.h"
#import "DYRightCollectionViewCell.h"

#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height

@interface DYOrderViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) UITableView *leftTableView;

@property (nonatomic,strong) UICollectionView *rightCollView;

//@property (nonatomic,strong) NSMutableArray *leftDataList;

//@property (nonatomic,strong) NSMutableArray *rightDataList;

//@property (nonatomic,strong) NSMutableArray *allRightDataList;
//当前被选中的ID值
@property (nonatomic,strong) DYLeftModel *currSelectModel;

@property (nonatomic,assign) BOOL isKeepScrollState;

@property (nonatomic,assign) BOOL isReturnLastOffset;

@property (nonatomic,assign) NSInteger selectIndex;
//左边列表的数据源
@property (nonatomic,strong) NSArray *leftNameArray;
//右边列表数据源
@property (nonatomic,strong) NSArray *rightArray;

@property (nonatomic,strong) NSArray *currArray;

@end

@implementation DYOrderViewController

-(void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_Arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnItemClick)];
    backBtnItem.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = backBtnItem;
}
-(void)backBtnItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"预约挂号";
    self.view.backgroundColor = [UIColor whiteColor];
    //是否允许右边滚动位置
    self.isKeepScrollState =YES;
    self.isReturnLastOffset = YES;
//    
//    self.leftNameArray = [NSMutableArray arrayWithArray:[DYLeftModel nameArray]];
//    self.rightArray = [NSMutableArray arrayWithArray:[DYRightCollModel hosArray]];
    
    self.leftNameArray = [DYLeftModel nameArray];
    self.rightArray = [DYRightCollModel hosArray];
//    self.rightArray = @[@[@"中西医结合科",@"妇科",@"放射科",@"产科",@"产前诊断",@"乳腺外科",@"新生儿科"],@[@"围产医学"],@[@"心内科",@"月经病门诊",@"自然流产专科",@"STD门诊",@"产后护理门诊",@"产科内分泌特需",@"母乳喂养门诊",@"中西医pcos",@"生殖免疫门诊",@"生殖免疫门诊",@"生殖免疫门诊",@"生殖免疫门诊",@"生殖免疫门诊",@"生殖免疫门诊",@"生殖免疫门诊",@"生殖免疫门诊"],@[@"月经病门诊",@"自然流产专科",@"STD门诊",@"产后护理门诊",@"中西医结合科",@"妇科",@"放射科",@"产科",@"乳腺外科",@"新生儿科"],@[@"乳腺外科",@"新生儿科",@"乳腺外科",@"新生儿科",@"母乳喂养门诊",@"中西医pcos",@"母乳喂养门诊",@"中西医pcos"],];
    
    self.currArray = self.rightArray[0];
    
//   //左边列表数据
//    for (int i = 0; i< self.leftNameArray.count; i++) {
//        DYLeftModel *leftModel = [[DYLeftModel alloc]init];
//        leftModel.tagID = i;
//        leftModel.tagName = [NSString stringWithFormat:@"%d层",i];
//        leftModel.tagName = leftModel.tagName;
//        [self.leftNameArray addObject:leftModel];
    
//        DYRightCollModel *rightModel = [[DYRightCollModel alloc]init];
    //右边列表数据
//        for (int j = 0; j< self.rightArray.count; j++) {
//            DYRightCollModel *rightModel = [[DYRightCollModel alloc]init];
//            rightModel.tagID = i;
//            rightModel.hosStyleID = j;
//            rightModel.hosStyleName = [NSString stringWithFormat:@"%d层%d",i,j];
//            
//        }
//        [self.rightArray addObject:rightModel];
//    }
//    
    
    self.selectIndex = 0;
    if (self.leftNameArray.count >0) {
        self.currSelectModel = [self.leftNameArray objectAtIndex:self.selectIndex];
        [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
        [self.leftTableView reloadData];
        //右边数据加载
        [self predicateDataSoure];
    }
    
    [self setTableView];
    [self setCollectionView];
    
    
    
    
    
}

-(void)setTableView
{
    self.leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 100, Height) style:UITableViewStylePlain];
    self.leftTableView.backgroundColor = [UIColor grayColor];
    self.leftTableView.showsHorizontalScrollIndicator = NO;
    self.leftTableView.showsVerticalScrollIndicator = NO;
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.separatorColor = [UIColor colorWithRed:52.0f/255.0f green:53.0f/255.0f blue:61.0f/255.0f alpha:0.7];
    
      self.leftTableView.tableFooterView = [[UIView alloc]init];//去掉不用的分割线
    [self.leftTableView registerClass:[DYLeftTableCell class] forCellReuseIdentifier:NSStringFromClass([DYLeftTableCell class])];
    
    if ([self.leftTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        self.leftTableView.layoutMargins = UIEdgeInsetsZero;
    }
    if ([self.leftTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        self.leftTableView.separatorInset = UIEdgeInsetsZero;
    }
    [self.view addSubview:self.leftTableView];

}


-(void)setCollectionView
{
    UICollectionViewFlowLayout *collLayout = [[UICollectionViewFlowLayout alloc]init];
    self.rightCollView = [[UICollectionView alloc]initWithFrame:CGRectMake(100 + 10, 64, Width - 100 -10 *2, Height-64) collectionViewLayout:collLayout];
    self.rightCollView.backgroundColor = [UIColor whiteColor];
    self.rightCollView.showsHorizontalScrollIndicator = NO;
    self.rightCollView.showsVerticalScrollIndicator = NO;
    self.rightCollView.delegate = self;
    self.rightCollView.dataSource = self;
    [self.rightCollView registerClass:[DYRightCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([DYRightCollectionViewCell class])];
    [self.view addSubview: self.rightCollView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.leftNameArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString *identifier = @"Cell";
//    DYLeftTableCell *leftCell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (leftCell == nil) {
//        leftCell = [[DYLeftTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
    
        DYLeftTableCell *leftCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DYLeftTableCell class]) forIndexPath:indexPath];
    leftCell.currLeftTagModel = [self.leftNameArray objectAtIndex:indexPath.row];
    leftCell.isLeftSelect = (leftCell.currLeftTagModel == self.currSelectModel);
    DYLeftModel *leftModel = self.leftNameArray[indexPath.row];
    leftCell.nameLabel.text = leftModel.tagName;
    leftCell.selectionStyle = UITableViewCellAccessoryNone;
    
    //修改表格行默认分隔线存在空隙问题
    if ([leftCell respondsToSelector:@selector(setLayoutMargins:)]) {
        leftCell.layoutMargins = UIEdgeInsetsZero;
    }
    if ([leftCell respondsToSelector:@selector(setSeparatorInset:)]) {
        leftCell.separatorInset = UIEdgeInsetsZero;
    }
    return leftCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    DYLeftTableCell * cell = (DYLeftTableCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    _selectIndex=indexPath.row;
    self.currSelectModel=[self.leftNameArray objectAtIndex:indexPath.row];

    
    
     [self.rightCollView reloadData];
    [self.leftTableView reloadData];
    
    [self predicateDataSoure]; //过滤右边的集合数据
    //处理点击在滚动置顶的问题
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    self.isReturnLastOffset = NO;
    if (self.isKeepScrollState) {
        [self.rightCollView scrollRectToVisible:CGRectMake(0, self.currSelectModel.offsetScorller, self.rightCollView.frame.size.width, self.rightCollView.frame.size.height) animated:NO];
    }else{
        [self.rightCollView scrollRectToVisible:CGRectMake(0, 0, self.rightCollView.frame.size.width, self.rightCollView.frame.size.height) animated:NO];
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
//  return self.rightArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.currArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    DYRightCollectionViewCell *collCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DYRightCollectionViewCell class]) forIndexPath:indexPath];
//    DYRightCollModel * array=self.rightArray[indexPath.section];
//    self.currArray = self.rightArray[self.selectIndex];
    DYRightCollModel *rightModel = self.currArray[indexPath.row];


    collCell.hosNameLabel.text = rightModel.hosStyleName;
    
    return collCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return [DYRightCollectionViewCell cellSize];
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DYRightCollModel *model = self.currArray[indexPath.row];
    NSLog(@"你选择的%@",model.hosStyleName);
}

-(void)predicateDataSoure
{
     //过滤右边的集合数据
    self.currArray  = self.rightArray[self.selectIndex];
    [self.rightCollView reloadData];
    
}

#pragma mark---记录滑动的坐标(把右边滚动的Y值记录在列表的一个属性中)

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.rightCollView]) {
        self.isReturnLastOffset=YES;
    }
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if ([scrollView isEqual:self.rightCollView]) {
       DYLeftModel * item=self.leftNameArray[self.selectIndex];
        item.offsetScorller=scrollView.contentOffset.y;
        self.isReturnLastOffset=NO;
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.rightCollView]) {
        DYLeftModel * item=self.leftNameArray[self.selectIndex];
        item.offsetScorller=scrollView.contentOffset.y;
        self.isReturnLastOffset=NO;
        
    }
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.rightCollView] && self.isReturnLastOffset) {
        DYLeftModel * item=self.leftNameArray[self.selectIndex];
        item.offsetScorller=scrollView.contentOffset.y;
    }
}


@end
